<?php

namespace app\admin\server\user;

use app\admin\model\user\UserModel;
use app\admin\model\user\UserWriterApplyModel;
use app\admin\ServerBase;
use think\facade\Db;
use think\Request;

class UserWriterApplyServer extends ServerBase
{
    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new UserWriterApplyModel();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        [$list,$count] = $this->model->getList($this->request,[],'*',$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }
    function getDetail(){
        $info = $this->model->getFindById($this->request->param('id'));
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
    function audit(){
        $id = $this->param['user_id']??0;
        $status = $this->param['status']??10;
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        if($info['status'] != 10){
            $this->ERROR='该状态不能审核';
            return false;
        }
        $update['remark'] = $this->param['remark']??'';
        if($status == 20){//审核通过
            $update['status'] = 20;
        }else{//审核失败
            $update['status'] = -10;
        }

        Db::startTrans();
        try {
            if($status == 20){
                //修改用户标记
                $user = (new UserModel())->getFindById($id);
                $user->save([
                    'is_writer'=>1,
                    'writer_name'=>$info['writer_name'],
                    'writer_head_img'=>$info['writer_head_img'],
                    'introduction'=>$info['introduction'],
                    'true_name'=>$info['true_name'],
                    'id_main'=>$info['id_main'],
                    'id_side'=>$info['id_side'],
                    'writer_mobile'=>$info['writer_mobile'],
                ]);
            }
            $a=$this->model->where('user_id',$id)->find();
            $a->save($update);
            Db::commit();
            return true;
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }

    }
}